package com.party.service.web.impl;

import java.io.File;
import java.util.Calendar;

import com.party.parBean.FileBean;
import com.party.service.web.FileWebService;
import org.ow2.util.base64.Base64;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * web模块提供的文件操作接口，清除当前时间减去指定时间(millisecond)之前的文件、通过文件UUID查找临时文件夹下的文件
 * 
 * @author beyond
 * 
 */
@Service("fileWebService")
public class FileWebServiceImpl implements FileWebService
{
    
    /**
     * 清除当前时间减去指定时间(millisecond)之前的文件
     * 
     * @param millisecond
     *            指定时间 单位(毫秒)
     * 
     * @return boolean 清除成功返回true,失败返回false
     */
    @Override
    public boolean emptyFilesByTimes(Long millisecond,HttpServletRequest request)
    {
        boolean flag = false;
        String webSrc = (this.getClass().getClassLoader().getResource("").getPath().toString()).replace("WEB-INF/classes/", "");
        String terminatedPath =webSrc+"/upload/files/";
        File file = new File(terminatedPath);
        if (!file.isDirectory())
        {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        Calendar cal = Calendar.getInstance();
        Long nowTimes = cal.getTimeInMillis();
        for (int i = 0; i < tempList.length; i++)
        {
            if (terminatedPath.endsWith(File.separator))
            {
                temp = new File(terminatedPath + tempList[i]);
            }
            else
            {
                temp = new File(terminatedPath + File.separator + tempList[i]);
            }
            
            Long timeDifference = nowTimes - temp.lastModified();
            if (timeDifference > millisecond)
            {
                temp.delete();
            }
        }
        flag = true;
        return flag;
    }
    
    /**
     * 通过UUID得到FileBean对象
     * 
     * @param UUID
     *            文件唯一标识号
     * 
     * @see com.party.parBean.FileBean
     * @return FileBean 返回FileBean对象
     */
    @Override
    public FileBean getFileByFileId(String UUID,HttpServletRequest request)
    {
        String webSrc = (this.getClass().getClassLoader().getResource("").getPath().toString()).replace("WEB-INF/classes/", "");
        String terminatedPath =webSrc+"/upload/files/";
        File file = new File(terminatedPath);
        if ("".equals(UUID))
        {
            throw new RuntimeException("UUID不能为空");
        }
        if (!file.isDirectory())
        {
            throw new RuntimeException(terminatedPath + "不是一个目录");
        }
        String[] tempList = file.list();
        File temp = null;
        FileBean fileBean = new FileBean();
        String fileName = "";
        String originalFilename = "";
        String filePrefix = "";
        String fileSuffix = "";
        for (int i = 0; i < tempList.length; i++)
        {
            
            if (terminatedPath.endsWith(File.separator))
            {
                String[] terminatedName = tempList[i].split("_");
                if (UUID.equals(terminatedName[0]))
                {
                    fileName = tempList[i];
                    originalFilename =
                            new String(Base64.decode(terminatedName[1]
                                    .toCharArray()));
                    Integer index = originalFilename.lastIndexOf(".");
                    Integer suffixIndex = terminatedName[1].lastIndexOf(".");
                    filePrefix = originalFilename;
                    
                    if (index != -1)
                    {
                        filePrefix = originalFilename.substring(0, index);
                        fileSuffix =
                                terminatedName[1].substring(suffixIndex + 1);
                    }
                    temp =
                            new File(terminatedPath + File.separator +
                                    tempList[i]);
                    break;
                }
                
            }
            else
            {
                String[] terminatedName = tempList[i].split("_");
                if (UUID.equals(terminatedName[0]))
                {
                    fileName = tempList[i];
                    originalFilename =
                            new String(Base64.decode(terminatedName[1]
                                    .toCharArray()));
                    Integer index = originalFilename.lastIndexOf(".");
                    Integer suffixIndex = terminatedName[1].lastIndexOf(".");
                    filePrefix = originalFilename;
                    
                    if (index != -1)
                    {
                        filePrefix = originalFilename.substring(0, index);
                        fileSuffix =
                                terminatedName[1].substring(suffixIndex + 1);
                    }
                    temp =
                            new File(terminatedPath + File.separator +
                                    tempList[i]);
                    
                    break;
                }
            }
        }
        
        fileBean.setUUID(UUID);
        fileBean.setOriginalFileName(originalFilename);
        fileBean.setFileName(fileName);
        fileBean.setFilePrefix(filePrefix);
        fileBean.setFileSuffix(fileSuffix);
        fileBean.setFileSize(temp.length());
        fileBean.setFile(temp);
        return fileBean;
    }
}
